<?php
/*
 * class.UserBase.php
 * 用户基础公共类
 * @version 0.1
 * @author samuel
 * @date 2009-05-18
 */
class UserBase {

    public $core ;
    public $params;
    public $error;

    function  __construct() {
        $this->core = $GLOBALS['CORE'];
        $this->params = $this->core->params;
        $this->error = "";
    }

    //构建SQL查询语句
    function buildAppendSql($arr_join = "",$arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => ''), $arr_groupby=""){
        $append_where = "";
		$append_orderby = "";
		$append_limit = "";
        $append_page_where = "";
        $append_join = "";
        $append_groupby = "";

        if(!empty($arr_join)){
            foreach($arr_join as $join){
                $append_join .= " LEFT JOIN `".$join['table']."` as ".$join['table_short']." ON ".$join['on'];
            }
        }

		if(!empty($arr_filter)){
			foreach($arr_filter as $filter){
                $join_append = "a";
                if($filter['join']) $join_append = $filter['join'];
				if($filter['match'] != 'like'){
                    if($filter['match'] == 'in'){
                        $append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` IN (" . $filter['value'] . ")";
                        if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` IN (" . $filter['value'] . ")";
                    }else{
                        $append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
                        if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` = '" . $filter['value'] . "'";
                    }
				}else{
					$append_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
                    if($join_append == "a") $append_page_where .= " AND ".$join_append.".`" . $filter['attr'] . "` LIKE '%" . $filter['value'] . "%'";
				}
			}
		}
		

		if(!empty($arr_orderby)){
			foreach($arr_orderby as $attr => $orderby){
				$append_orderby .= " ORDER BY a.`" . $attr . "` " . $orderby;
			}
		}

		if(!empty($arr_limit)){
            $arr_limit['offset'] = ($arr_limit['page'] - 1) * $arr_limit['pagesize'];
            $append_limit .= " LIMIT " . $arr_limit['offset'] . "," . $arr_limit['pagesize'];
        }

        if(!empty($arr_groupby)){
            foreach($arr_groupby as $groupby){
                $append_groupby .= " GROUP BY a.`".$groupby."`";
            }
        }

        return array('where' => $append_where, 'page_where' => $append_page_where, 'order' => $append_orderby, 'limit' => $append_limit, 'join'=> $append_join, 'group'=>$append_groupby);
    }

    //获取信息列表
    function getInfoList($table_info, $arr_join = "", $arr_filter = "", $arr_orderby = "", $arr_limit = array('page' => 1, 'pagesize' => 20, 'url' => ''), $count=false, $distinct=false, $arr_groupby=""){
        $arr_sql_appends = $this->buildAppendSql($arr_join, $arr_filter, $arr_orderby, $arr_limit, $arr_groupby);

        $append_where = $arr_sql_appends['where'];
        $append_orderby = $arr_sql_appends['order'];
        $append_limit = $arr_sql_appends['limit'];
        $append_page_where = $arr_sql_appends['page_where'];
        $append_join = $arr_sql_appends['join'];
        $append_groupby = $arr_sql_appends['group'];

        $table_name = $table_info['table'];
        $table_primary = $table_info['primary'];

        //分页处理
        if($arr_limit)
        $pager = $this->core->utility->pager($table_name, $table_primary, $append_page_where, $arr_limit);


        if($count && $distinct){
            $sql = "SELECT count(distinct(".$distinct.")) ";
        }elseif($count && !$distinct){
            $sql = "SELECT count(*) ";
        }elseif(!$count && $distinct){
            $sql = "SELECT * ";
        }
        //$sql = "SELECT * ";
		$sql .= " FROM `".$table_name."` as a ";
        $sql .= $append_join;
		$sql .= " WHERE 1" . $append_where . $append_groupby . $append_orderby . $append_limit;
        //echo $sql."<br/>";;
        
        if($count) return $this->core->db->get_var($sql);

        $arr_infolists = array();

        $lists = $this->core->db->get_results($sql);


        if(!empty($lists)){
            foreach($lists as $key => $list){
                foreach($list as $k => $v){
                    $arr_infolists[$key][$k] = $v;
                }
            }
        }

        return array('list' => $arr_infolists, 'pager' => $pager);
    }

    //添加新信息
	function addNewInfo($table, $info){
		if(!$info){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$arr_append_sql = array();
		$append_sql = "";
        $append_check = "";
		foreach($info as $attr => $value){
			$arr_append_sql[] = " `".$attr."` = '".$value."'";
            $append_check .= " AND `".$attr."` = '".$value."'";
		}
		$append_sql = @implode(',', $arr_append_sql);

        $sql = "SELECT count(*) FROM `".$table."` WHERE 1" . $append_check;
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该信息，添加失败！";
            return false;
        }

		$sql = "INSERT INTO `".$table."` SET ".$append_sql;
		$res = $this->core->db->query($sql);
		return $res;
	}

    //获取信息
    function getInfo($table_info,$id){
        if(!$id) return false;

        $table_name = $table_info['table'];
        $table_primary = $table_info['primary'];
        
        $sql = "SELECT * FROM `".$table_name."` WHERE `".$table_primary."`=" . $id;
        //echo $sql;
        $res = $this->core->db->get_row($sql);
        $arr_info = array();
        if($res){
            foreach($res as $k => $v){
                $arr_info[$k] = $v;
            }
        }
        return $arr_info;
    }

    //更新信息
    function updateInfo($update_table, $update_primary , $update_id, $update_Info){
		if(!$update_table || !$update_id || !$update_Info){
            $this->error .= "参数不合法，请检查！";
            return false;
        }

		$arr_append_update = array();
		$append_update = "";
        $append_check = "";
		foreach($update_Info as $attr => $value){
			$arr_append_update[] = " `".$attr."` = '".$value."'";
            $append_check .= " AND `".$attr."` = '".$value."'";
		}
		$append_update = @implode(',', $arr_append_update);

        $sql = "SELECT count(*) FROM `".$update_table."` WHERE 1" . $append_check . " AND `".$update_primary."` != '".$update_id."'";
        $tag = $this->core->db->get_var($sql);
        if($tag){
            $this->error .= "已经存在该值，更新失败！";
            return false;
        }

		$sql = "UPDATE `".$update_table."` SET ".$append_update." WHERE `".$update_primary."`='" . $update_id ."'";
		$res = $this->core->db->query($sql);
		return $res;
	}

    //删除信息
	function delInfo($del_table, $del_primary, $del_id){
		if(!$del_table || !$del_primary || !$del_id){
            $this->error .= "请选择要删除的选项！";
            return false;
        }

		$sql = "DELETE FROM `".$del_table."` WHERE `".$del_primary."`=" . $del_id;
		$res = $this->core->db->query($sql);
		return $res;
	}

    //获取用户组列表
    function getUserGroupList(){
        $sql = "SELECT `UserGroupID`, `UserGroupName` FROM `fun_user_group`";
        $res = $this->core->db->get_results($sql);
        if($res){
            foreach($res as $group){
                $arr_res[] = array(
                    'UserGroupID' => $group->UserGroupID,
                    'UserGroupName' => $group->UserGroupName,
                );
            }
        }
        return $arr_res;
    }

    //获取省份简单列表
    function getSellerAllProvinceList(){
        $sql = "SELECT `SellerProvinceID`, `SellerProvinceChar`, `SellerProvinceNameCN` FROM `fun_seller_province` ORDER BY `SellerProvinceChar` ASC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $province){
				$arr_res[] = array(
									'pid' => $province->SellerProvinceID,
									'pchar' => $province->SellerProvinceChar,
									'pname' => $province->SellerProvinceNameCN
								);
			}
		}
		return $arr_res;
    }

    //获取城市简单列表
    function getSellerAllCityList($pid = ""){
        if($pid) $append = " AND `SellerProvinceID`='".(int)$pid."'";
        $sql = "SELECT `SellerCityID`, `SellerCityNameCN`, `SellerCityNo` FROM `fun_seller_city`
                WHERE 1 ".$append." ORDER BY `SellerCityNo` ASC";
		$res = $this->core->db->get_results($sql);
		$arr_res = array();
		if($res){
			foreach($res as $province){
				$arr_res[] = array(
									'cid' => $province->SellerCityID,
									'cname' => $province->SellerCityNameCN,
									'cno' => $province->SellerCityNo
								);
			}
		}
		return $arr_res;
    }
}
?>
